package com.work.yshop.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;
import com.work.yshop.adapter.ProductAdapter;
import com.work.yshop.model.Product;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索商品Activity
 */
public class SearchActivity extends AppCompatActivity {

    private EditText etSearch;
    private ImageButton btnSearch;
    private RecyclerView recyclerView;
    private LinearLayout emptyView;
    private TextView tvEmptyMessage;
    private ProductAdapter productAdapter;
    private List<Product> productList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        // 初始化视图
        initViews();

        // 设置工具栏
        setupToolbar();

        // 设置RecyclerView
        setupRecyclerView();

        // 设置搜索功能
        setupSearchFunctionality();
    }

    private void initViews() {
        etSearch = findViewById(R.id.et_search);
        btnSearch = findViewById(R.id.btn_search);
        recyclerView = findViewById(R.id.recycler_view);
        emptyView = findViewById(R.id.empty_view);
        tvEmptyMessage = findViewById(R.id.tv_empty_message);
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    private void setupRecyclerView() {
        productList = new ArrayList<>();
        productAdapter = new ProductAdapter();
        productAdapter.setData(productList);
        
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setAdapter(productAdapter);
        
        // 初始状态显示空视图
        updateEmptyViewVisibility();
    }

    private void setupSearchFunctionality() {
        // 搜索按钮点击事件
        btnSearch.setOnClickListener(v -> performSearch());

        // 键盘搜索按钮点击事件
        etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                performSearch();
                return true;
            }
            return false;
        });
    }

    private void performSearch() {
        String query = etSearch.getText().toString().trim();
        
        if (query.isEmpty()) {
            return;
        }
        
        // 清空之前的搜索结果
        productList.clear();
        
        // 模拟搜索操作
        // 实际应用中，这里应该调用API或数据库查询
        searchProducts(query);
        
        // 更新UI
        productAdapter.notifyDataSetChanged();
        updateEmptyViewVisibility();
    }
    
    private void searchProducts(String query) {
        // 模拟从数据库或API获取搜索结果
        // 这里只是示例，实际应用中应该实现真正的搜索逻辑
        
        // 如果没有搜索结果，可以设置空视图的消息
        if (productList.isEmpty()) {
            tvEmptyMessage.setText("没有找到与 \"" + query + "\" 相关的商品");
        }
    }
    
    private void updateEmptyViewVisibility() {
        if (productList.isEmpty()) {
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}